/**
 * 
 */
package systole.persistence.brokersDB;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import systole.domain.signals.RawSignal;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.RawSignalBroker;
import systole.view.messages.ErrorMessages;

/**
 * @author jmj
 *
 */
public class RawSignalBrokerDB extends BrokerDB implements RawSignalBroker {

    /**
     *
     */
    public RawSignalBrokerDB() {
        super();
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.RawSignalBroker#getRawSignalById(long)
     */
    @Override
    public RawSignal getRawSignalById(Integer id) throws ExceptionDAO {
        try {
            this.logger.logDebug("getting rawSignal instance");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            RawSignal instance = (RawSignal) currentSession.get(RawSignal.class, id);
            this.logger.logDebug("get rawSignal successfully");
            return instance;
        } catch (HibernateException e) {
            this.logger.logError("error on get rawSignal by id, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo obtener la Señal", e.fillInStackTrace());
        }
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.RawSignalBroker#insert(systole.domain.signals.RawSignal)
     */
    @Override
    public void insert(RawSignal rawSignal) throws ExceptionDAO {
        try {
            this.logger.logDebug("save rawSignal ");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(rawSignal);
            this.logger.logDebug("save rawSignal successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on save rawSignal, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }

    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.RawSignalBroker#update(systole.domain.signals.RawSignal)
     */
    @Override
    public void update(RawSignal rawSignal) throws ExceptionDAO {
        try {
            this.logger.logDebug("Updating rawSignal");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.update(rawSignal);
            this.logger.logDebug("rawSignal updated");
        } catch (HibernateException e) {
            this.logger.logError("error on update rawSignal, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }
}
